<?php

namespace common\models\views;

use Yii;

/**
 * This is the model class for table "view_special_discount".
 *
 * @property int $id
 * @property string $name 活动名称
 * @property string $description 说明
 * @property string $begin_time 活动开始时间
 * @property string $end_time 活动结束时间
 * @property int $status 活动状态
 * @property int $scope 应用范围类型：0商品，1店铺，2品类，3品牌，4运费，5平台
 * @property string $discount_id 优惠规则ID
 * @property string $limit 限制数量
 * @property int $unit 限制单位：0金额，1件
 * @property string $discount 折扣金额/比例
 * @property int $radix 折扣单位：0金额，100比例基数
 * @property int $model_id 优惠项目ID
 */
class ViewSpecialDiscount extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'view_special_discount';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'begin_time', 'end_time', 'status', 'scope', 'discount_id', 'limit', 'unit', 'discount', 'radix', 'model_id'], 'integer'],
            [['name', 'description'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '活动名称',
            'description' => '说明',
            'begin_time' => '活动开始时间',
            'end_time' => '活动结束时间',
            'status' => '活动状态',
            'scope' => '应用范围类型：0商品，1店铺，2品类，3品牌，4运费，5平台',
            'discount_id' => '优惠规则ID',
            'limit' => '限制数量',
            'unit' => '限制单位：0金额，1件',
            'discount' => '折扣金额/比例',
            'radix' => '折扣单位：0金额，100比例基数',
            'model_id' => '优惠项目ID',
        ];
    }

    public static function findAvalid($scopes = []) {
        $scopevalue = [
            'goods_ids' => 0,
            'shop_ids' => 1,
            'brand_ids' => 2,
            'type_ids' => 3,
        ];
        $model = static::find()->where([
            'and',
            ['status' => 10],
            ['<', 'begin_time', time()],
            ['>', 'end_time', time()],
        ])->orderBy(['scope' => SORT_ASC, 'begin_time' => SORT_ASC, 'end_time' => SORT_ASC]);

        $condition = [];
        foreach ($scopes as $key => $vls) {
            $temp = [
                'scope' => $scopevalue[$key]
            ];
            if ($vls) {
                $temp['model_id'] = array_unique($vls);
            }
            $condition[] = $temp;
        }
        if (!empty($condition)) {
            array_unshift($condition, 'or');
            $model->andWhere($condition);
        }
        $activity = $model->all();
        $data = [];

        foreach ($activity as $discount) {
            $scope = $discount->scope;

            $discountid = $discount->discount_id;
            if (!isset($data[$scope][$discountid])) {
                $temp = [
                    'limit' => $discount->limit,
                    'unit' => $discount->unit, //0为元，1为件
                    'discount' => $discount->discount,
                    'radix' => $discount->radix,
                    'model_ids' => []
                ];
                $data[$scope][$discountid] = $temp;
            }
            $data[$scope][$discountid]['model_ids'][] = $discount->model_id;
        }

        return $data;
    }

}
